<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小账单物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\property\bill;

use think\App;
use app\controller\property\Base;
use app\validate\property\bill\PropertyBillDayValidate;
use app\common\repositories\property\basic\PropertyBasicRepository;
use app\common\repositories\property\bill\PropertyBillDayRepository;

class PropertyBillDay extends Base
{

    protected $repository;

    public function __construct(App $app, PropertyBillDayRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyBillDayAdd'),
            'editAuth' => property_auth('propertyBillDayEdit'),
            'delAuth' => property_auth('propertyBillDayDel'),
            'detailAuth' => property_auth('propertyBillDayDetail'),
        ]);
    }


    /**
     * 物业账单列表
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-04-30 11:37
     */
    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'property_id' => '',
                'day_time' => '',
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        /** @var PropertyBasicRepository $propertyRepository */
        $propertyRepository = app()->make(PropertyBasicRepository::class);
        $propertyData = $propertyRepository->getPropertyData($this->request->propertyId, ['is_show' => 1]);
        return $this->fetch('property/bill/day/list', [
            'propertyData' => $propertyData,
        ]);
    }


    /**
     * 添加物业账单
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-04-30 11:37
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'property_id' => '',
                'day_time' => '',
                'remark' => '',
            ]);
            validate(PropertyBillDayValidate::class)->scene('add')->check($param);
            $countNum = $this->repository->getSearch([])
                ->where('day_time', $param['day_time'])
                ->where('property_id', $param['property_id'])
                ->count();
            if ($countNum >= 1) {
                return $this->error($param['day_time'] . '己存在');
            }
            try {
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加物业日账单失败', $e);
                return $this->error('网络失败');
            }
        } else {
            /** @var PropertyBasicRepository $propertyRepository */
            $propertyRepository = app()->make(PropertyBasicRepository::class);
            $propertyData = $propertyRepository->getPropertyData($this->request->propertyId, ['is_show' => 1]);
            return $this->fetch('property/bill/day/add', [
                'propertyData' => $propertyData,
            ]);
        }
    }

    /**
     * 编辑物业账单
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-04-30 11:38
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'remark' => '',
            ]);
            validate(PropertyBillDayValidate::class)->scene('edit')->check($param);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑物业日账单失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('property/bill/day/edit', [
                'info' => $info,
            ]);
        }
    }

    /**
     * 物业账单详情
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-04-30 11:38
     */
    public function detail()
    {
        $billId = (int)$this->request->param('bill_id');
        if ($billId <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($billId);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('property/bill/day/detail', [
            'info' => $info,
        ]);
    }

    /**
     * 删除物业账单
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-04-30 11:38
     */
    public function del()
    {
        $ids = (array)$this->request->param('ids');
        $param = $this->request->param([
            'remark' => '',
        ]);
        validate(PropertyBillDayValidate::class)->scene('del')->check($param);
        $remark = $param['remark'];
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId, $remark);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除物业日账单失败', $e);
            return $this->error('网络失败');
        }
    }


}